package com.yaorange.jk.web.state;

import com.yaorange.jk.entity.EchartBarVo;
import com.yaorange.jk.entity.EchartPieVo;
import com.yaorange.jk.entity.Product;
import com.yaorange.jk.utils.FastJsonUtil;
import com.yaorange.jk.web.BaseAction;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONUtil;
import org.hibernate.*;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

public class StatChartAction extends BaseAction {
    @Autowired
    private SessionFactory sessionFactory;

    String legendData ;
    String seriesData;
    public String factorysale(){

        return "factorysale";
    }

    public String highPriceProduct(){

        return "highPriceProduct";
    }
    public String getHighPriceProductJson(){
        Session session = sessionFactory.openSession();
        // Query query = session.createQuery("from Product p order by p.price");
        Criteria c = session.createCriteria(Product.class);
        c.addOrder(Order.desc("price"));
        c.setMaxResults(10);
        List<Product> list = c.list();
        EchartBarVo vo = new EchartBarVo();
        String[] x = new String[list.size()];
        String[] y = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Product p = list.get(i);
            x[i] = p.getProductNo();
            y[i] = p.getPrice().toString();
        }
        vo.setXAxis(x);
        vo.setSeriesData(y);
        String json = FastJsonUtil.toJSONString(vo);
        FastJsonUtil.write_json(getResponse(),json);
        return  NONE;
    }


    public String getFactorySaleJson(){
        EchartBarVo vo = new EchartBarVo();
        Session session = sessionFactory.openSession();
        SQLQuery sqlQuery = session.createSQLQuery("select c.factory_name,sum(c.cnumber) sale from contract_product_c c,contract_c ct  where c.contract_id = CT.contract_id and CT.state=2 and c.factory_name is not null group by c.factory_name");
        List<Object[]> objects = sqlQuery.list();

        String[] xAxis = new String[objects.size()];
        String[] seriesData = new String[objects.size()];
        for (int i = 0 ;i < objects.size();i++) {
            xAxis[i] = (String) objects.get(i)[0];
            seriesData[i] = objects.get(i)[1].toString();
        }
        vo.setXAxis(xAxis);
        vo.setSeriesData(seriesData);
        session.close();

        String json = FastJsonUtil.toJSONString(vo);
        FastJsonUtil.write_json(ServletActionContext.getResponse(),json);
        return NONE;
    }

    public String onlineinfo(){

        return "onlineinfo";
    }


    public String getOnlineinfoJson(){
        EchartBarVo vo = new EchartBarVo();
        Session session = sessionFactory.openSession();
        SQLQuery sqlQuery = session.createSQLQuery("select o.a1,nvl(lc.counts,0) counts from  ONLINE_info_t o \n" +
                "LEFT JOIN \n" +
                "(select to_char(l.login_time,'hh24') hours,count(l.login_time) counts from login_log_p l group by to_char(l.login_time,'hh24')) lc\n" +
                "ON\n" +
                "o.a1 = lc.hours order by o.a1 ");
        List<Object[]> objects = sqlQuery.list();

        String[] xAxis = new String[objects.size()];
        String[] seriesData = new String[objects.size()];
        for (int i = 0 ;i < objects.size();i++) {
            xAxis[i] = (String) objects.get(i)[0];
            seriesData[i] = objects.get(i)[1].toString();
        }
        vo.setXAxis(xAxis);
        vo.setSeriesData(seriesData);
        session.close();

        String json = FastJsonUtil.toJSONString(vo);
        FastJsonUtil.write_json(ServletActionContext.getResponse(),json);
        return NONE;
    }


    public String productsale(){

        Session session = sessionFactory.openSession();
        SQLQuery sqlQuery = session.createSQLQuery("select * from (select p.product_no,sum(p.cnumber) sale from export_product_c p group by  p.product_no )  p2 where rownum < 16 order by p2.sale desc");
        List<Object[]> objects = sqlQuery.list();

        String[] legendDatas = new String[objects.size()];

        EchartPieVo[] echartPieVos = new EchartPieVo[objects.size()];

        //循环遍历，填充数组
        for(int i = 0 ;i < objects.size();i++)
        {
            Object[] obj = objects.get(i);
            legendDatas[i] = (String) obj[0];


            EchartPieVo vo = new EchartPieVo();
            vo.setName((String) obj[0]);
            vo.setValue(obj[1].toString());

            echartPieVos[i]=vo;
        }


        legendData = FastJsonUtil.toJSONString(legendDatas);
        seriesData = FastJsonUtil.toJSONString(echartPieVos);
        return "productsale";
    }

    public String getLegendData() {
        return legendData;
    }

    public void setLegendData(String legendData) {
        this.legendData = legendData;
    }

    public String getSeriesData() {
        return seriesData;
    }

    public void setSeriesData(String seriesData) {
        this.seriesData = seriesData;
    }
}
